package com.yanqu.road.server.cmd.activity.commonteam;

import com.yanqu.road.entity.activity.commonteam.CommonTeamInitUserData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.CommonTeamProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamActivity;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C2_COMMON_TEAM_GET_USER_DATA, desc = "通用组队-获取玩家数据")
public class CrossCommonTeamGetUserDataCmd extends CrossUnionCommand {

    private long playerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        playerId = packet.getPlayerId();
        return true;
    }
    
    @Override
    public void execute(YanQuMessage packet) throws Exception {
        CommonTeamProto.CommonTeamGetUserDataReqMsg reqMsg = CommonTeamProto.CommonTeamGetUserDataReqMsg.parseFrom(packet.getBytes());
        CommonTeamProto.CommonTeamGetUserDataRespMsg.Builder respMsg = CommonTeamProto.CommonTeamGetUserDataRespMsg.newBuilder();
        //获取活动
        CommonTeamActivity activity = CommonTeamMgr.getActivity(reqMsg.getActivityId(), groupId);
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_COMMON_TEAM_TEAM_ACTIVITY_NOT_EXIST);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_COMMON_TEAM_GET_USER_DATA, respMsg));
            return;
        }
        //存储userBase
        if(reqMsg.hasUserInfo()){
            UserBaseInfo userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getUserInfo());
            CrossUserMgr.updateUserBaseInfo(packet.getPlayerId(), userBaseInfo);
        }
        //创建玩家
        activity.getUserDataOrCreate(packet.getPlayerId(), reqMsg.getPower());
        //同步
        activity.syncUserData(packet.getPlayerId());
    }

    @Override
    public void sendData() {}
}
